//
//  LKMessageDetailViewController.m
//  LKIntelligentVideoProject
//
//  Created by RB8 on 2019/2/27.
//  Copyright © 2019年 she1323. All rights reserved.
//

#import "LKMessageDetailViewController.h"
#import "LKMessageListCell.h"
#import "WHWebViewController.h"
@interface LKMessageDetailViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *dataArray;
@property(nonatomic,strong)NSMutableArray *contensArray;
@end

@implementation LKMessageDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 120, 44)];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = titleLabel;
    if ([_model.equipmentType isEqual:@"1"]) {
//            self.dataArray = [[NSMutableArray alloc] initWithArray:@[@"设备名称",@"告警内容",@"告警等级",@"报警时间",@"处理状态"]];
//            self.contensArray = [[NSMutableArray alloc] initWithArray:@[self.model.sourceName,self.model.eventTypeName,[self alarmGradeStringAccroingEventLevel:self.model.eventLevel],[NSString timestampSwitchTime:[_model.createTime integerValue] / 1000 andFormatter:@"yyyy-MM-dd HH:mm:ss"],[self backCoverDealStatus:@""]]];
    }else if ([_model.equipmentType isEqual:@"2"]){
            self.dataArray = [[NSMutableArray alloc] initWithArray:@[@"设备名称",@"告警内容",@"告警等级",@"报警时间"]];
            self.contensArray = [[NSMutableArray alloc] initWithArray:@[self.model.sourceName,self.model.eventTypeName,[self videoAndGuardAlarmGradeStringAccroingEventLevel:self.model.eventLevel],[NSString timestampSwitchTime:[_model.createTime integerValue] / 1000 andFormatter:@"yyyy-MM-dd HH:mm:ss"]]];
            titleLabel.text = self.model.sourceName;
    }else if ([_model.equipmentType isEqual:@"3"]){
            self.dataArray = [[NSMutableArray alloc] initWithArray:@[@"设备名称",@"告警内容",@"告警等级",@"报警时间",@"是否有抓图"]];
        self.contensArray = [[NSMutableArray alloc] initWithArray:@[self.model.sourceName,self.model.eventTypeName,[self videoAndGuardAlarmGradeStringAccroingEventLevel:self.model.eventLevel],[NSString timestampSwitchTime:[_model.createTime integerValue] / 1000 andFormatter:@"yyyy-MM-dd HH:mm:ss"],[self.model.eventState isEqual:@"4"] ? @"查看":@"无"]];
            titleLabel.text = self.model.sourceName;
    }else{
        self.dataArray = [[NSMutableArray alloc] initWithArray:@[@"设备名称",@"告警内容",@"告警等级",@"报警时间",@"处理状态"]];
        self.contensArray = [[NSMutableArray alloc] initWithArray:@[self.model.alarmAddr,self.model.alarmName,[self alarmGradeStringAccroingEventLevel:self.model.alarmGrade],[NSString timestampSwitchTime:[_model.createTime integerValue] / 1000 andFormatter:@"yyyy-MM-dd HH:mm:ss"],[self backCoverDealStatus:self.model.alarmStatus]]];
            titleLabel.text = self.model.alarmAddr;
    }
    [self setNavgationBackBtn];
//    self.title = @"详情";
    [self setupTableview];
    [self.tableView reloadData];
    // Do any additional setup after loading the view.
}
//返回井盖的处理状态信息
-(NSString *)backCoverDealStatus:(NSString *)status{
    if ([status isEqual:@"0"]) {
        return @"未处理";
    }else if ([status isEqual:@"1"]){
        return @"处理中";
    }else if ([status isEqual:@"2"]){
        return @"已处理";
    }
    return @"";
}
/**
 门禁和监控的报警等级

 @param level 等级
 @return 对应状态
 */
-(NSString *)videoAndGuardAlarmGradeStringAccroingEventLevel:(NSString *)level{
    
    switch ([level integerValue]) {
        case 0:
        {
            return @"普通事件";
        }
            break;
        case 2:
        {
            return @"低级报警";
        }
            break;
        case 3:
        {
            return @"高级报警";
        }
            break;
        case 4:
        {
            return @"高级报警";
        }
            break;
        default:
            break;
    }
    return @"";
}
//井盖的报警等级
-(NSString *)alarmGradeStringAccroingEventLevel:(NSString *)level{
    
    switch ([level integerValue]) {
        case 1:
        {
            return @"预警";
        }
            break;
        case 2:
        {
            return @"一般告警";
        }
            break;
        case 3:
        {
            return @"重要告警";
        }
            break;
        case 4:
        {
            return @"紧急告警";
        }
            break;
        default:
            break;
    }
    return @"";
}
/**
 设置tableview
 */
-(void)setupTableview{

    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, NAVIGATION_BAR_HEIGHT, WIDTH, HEIGHT - NAVIGATION_BAR_HEIGHT) style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.emptyDataSetSource = self;
    _tableView.emptyDataSetDelegate = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    [self.view addSubview:_tableView];
}
/**
 设置返回按钮样式
 */
-(void)setNavgationBackBtn{
    UIButton *button = [[UIButton alloc] init];
    [button setImage:[UIImage imageNamed:@"mapBack"] forState:UIControlStateNormal];
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [button setTitle:@" 返回" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button.bounds = CGRectMake(0, 0, 70, 30);
    button.contentEdgeInsets = UIEdgeInsetsMake(0, -5, 0, 0);
    button.titleLabel.font = [UIFont systemFontOfSize:16];
    [button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
}

- (void)back
{
    NSArray *viewcontrollers = self.navigationController.viewControllers;
    if (viewcontrollers.count > 1) {
        if ([viewcontrollers objectAtIndex:viewcontrollers.count - 1] == self) {
            //push方式
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
    else{
        //present方式
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellId = @"cellId";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellId];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.textLabel.text = self.dataArray[indexPath.section];
    cell.detailTextLabel.text = self.contensArray[indexPath.section];
    if ([self.contensArray[indexPath.section] isEqual:@"查看"]) {
        cell.detailTextLabel.textColor = [UIColor blueColor];
    }else{
        cell.detailTextLabel.textColor = [UIColor lightGrayColor];
    }
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView = [[UIView alloc] init];
    headerView.backgroundColor = [UIColor whiteColor];
    if (section == 0) {
        UIImageView *imageV = [[UIImageView alloc] init];
        [headerView addSubview:imageV];
        if ([_model.equipmentType isEqual:@"1"]) {
        }else if ([_model.equipmentType isEqual:@"2"]){
            imageV.image = [UIImage imageNamed:@"menjin"];
            
        }else if ([_model.equipmentType isEqual:@"3"]){
            imageV.image = [UIImage imageNamed:@"WechatIMG3"];
        }else{
            if ([_model.alarmStatus isEqual:@"2"]) {
                imageV.image = [UIImage imageNamed:@"WechatIMG1"];
            }else{
                imageV.image = [UIImage imageNamed:@"jinggai"];
            }
        }
        [imageV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(headerView);
            make.size.mas_equalTo(CGSizeMake(56, 56));
        }];
    }
    return headerView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 80.f;
    }
    return 0.0000000001f;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *footerView = [[UIView alloc] init];
    footerView.backgroundColor = [UIColor lightGrayColor];
    return footerView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.5f;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([self.contensArray[indexPath.section] isEqual:@"查看"]) {
        WHWebViewController *webVc = [[WHWebViewController alloc] init];
        webVc.url = self.model.eventPic;
        webVc.showClose = YES;
        [self.navigationController pushViewController:webVc animated:YES];
    }else{
        
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
